package org.jeecg.modules.lab.items.temp2.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Basic;
import org.jeecg.modules.lab.items.temp2.service.ILabItemsTemp2BasicService;
import org.jeecg.modules.lab.items.vo.LabItemsTargetPage;
import org.jeecg.modules.lab.items.vo.LabItemsTargetVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 实验项目模板-基础信息
 * @Author: jeecg-boot
 * @Date:   2023-11-15
 * @Version: V1.0
 */
@Api(tags="实验项目模板-基础信息")
@RestController
@RequestMapping("/items/labItemsTemp2Basic")
@Slf4j
public class LabItemsTemp2BasicController extends JeecgController<LabItemsTemp2Basic, ILabItemsTemp2BasicService> {
	@Autowired
	private ILabItemsTemp2BasicService labItemsTemp2BasicService;
	@Autowired
	private ILabCronJobService labCronJobService;
	
	/**
	 * 分页列表查询
	 *
	 * @param labItemsTemp2Basic
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "实验项目模板-基础信息-分页列表查询")
	@ApiOperation(value="实验项目模板-基础信息-分页列表查询", notes="实验项目模板-基础信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<LabItemsTemp2Basic>> queryPageList(LabItemsTemp2Basic labItemsTemp2Basic,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="20") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LabItemsTemp2Basic> queryWrapper = QueryGenerator.initQueryWrapper(labItemsTemp2Basic, req.getParameterMap());
		queryWrapper.orderByAsc("field_sort");
		Page<LabItemsTemp2Basic> page = new Page<LabItemsTemp2Basic>(pageNo, pageSize);
		IPage<LabItemsTemp2Basic> pageList = labItemsTemp2BasicService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param labItemsTemp2Basic
	 * @return
	 */
	@AutoLog(value = "实验项目模板-基础信息-添加")
	@ApiOperation(value="实验项目模板-基础信息-添加", notes="实验项目模板-基础信息-添加")
//	@RequiresPermissions("basic:lab_items_temp2_basic:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody LabItemsTemp2Basic labItemsTemp2Basic) {
		labItemsTemp2BasicService.save(labItemsTemp2Basic);

		if (MyEnum.sync){
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_items_temp2_basic","0",time,1);
			labCronJobService.save(job);
		}
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param labItemsTemp2Basic
	 * @return
	 */
	@AutoLog(value = "实验项目模板-基础信息-编辑")
	@ApiOperation(value="实验项目模板-基础信息-编辑", notes="实验项目模板-基础信息-编辑")
//	@RequiresPermissions("basic:lab_items_temp2_basic:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody LabItemsTemp2Basic labItemsTemp2Basic) {
		labItemsTemp2Basic.setSynFlag("1");
		labItemsTemp2BasicService.updateById(labItemsTemp2Basic);

		if (MyEnum.sync){
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_items_temp2_basic","1",time,1);
			labCronJobService.save(job);
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "实验项目模板-基础信息-通过id删除")
	@ApiOperation(value="实验项目模板-基础信息-通过id删除", notes="实验项目模板-基础信息-通过id删除")
//	@RequiresPermissions("basic:lab_items_temp2_basic:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		labItemsTemp2BasicService.removeById(id);

		if (MyEnum.sync){
			long time = new Date().getTime();
			String condition="id='"+id+"'";
			LabCronJob job = new LabCronJob("lab_items_temp2_basic","2",condition,time,1);
			labCronJobService.save(job);
		}
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "实验项目模板-基础信息-批量删除")
	@ApiOperation(value="实验项目模板-基础信息-批量删除", notes="实验项目模板-基础信息-批量删除")
//	@RequiresPermissions("basic:lab_items_temp2_basic:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.labItemsTemp2BasicService.removeByIds(Arrays.asList(ids.split(",")));

		if (MyEnum.sync) {
			long time = new Date().getTime();
			String[] split = ids.split(",");
			labCronJobService.saveCronBatch("lab_items_temp2_basic","2",time,split);
		}
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "实验项目模板-基础信息-通过id查询")
	@ApiOperation(value="实验项目模板-基础信息-通过id查询", notes="实验项目模板-基础信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<LabItemsTemp2Basic> queryById(@RequestParam(name="id",required=true) String id) {
		LabItemsTemp2Basic labItemsTemp2Basic = labItemsTemp2BasicService.getById(id);
		if(labItemsTemp2Basic==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(labItemsTemp2Basic);
	}

	/**
	 * 添加基础
	 *
	 * @param labItemsTargetPage
	 * @return
	 */
	@AutoLog(value = "实验项目模板-添加基础")
	@ApiOperation(value = "实验项目模板-添加基础", notes = "实验项目模板-添加基础")
	@PostMapping(value = "/addBasic")
	public Result<String> addBasic(@RequestBody LabItemsTargetPage labItemsTargetPage) {
		String itemsId = labItemsTargetPage.getItemsId();
		//查询已有指标
		QueryWrapper<LabItemsTemp2Basic> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("items_id", itemsId);
		List<LabItemsTemp2Basic> labItemsTemp2BasicList = labItemsTemp2BasicService.list(queryWrapper);
		int num = labItemsTemp2BasicList.size();
		List<LabItemsTargetVo> itemsTargetList = labItemsTargetPage.getItemsTargetList();
		if (num > 0) {
			for (int i = 0; i < itemsTargetList.size(); i++) {
				LabItemsTargetVo targetVo = itemsTargetList.get(i);
				boolean bool = true;
				for (LabItemsTemp2Basic basic : labItemsTemp2BasicList) {
					if (targetVo.getFieldName().equals(basic.getFieldName())) {
						bool = false;
						break;
					}
				}
				if (bool) {
					LabItemsTemp2Basic basic = new LabItemsTemp2Basic();
					basic.setItemsId(itemsId);
					basic.setFieldName(targetVo.getFieldName());
					basic.setFieldCode(targetVo.getFieldCode());
					basic.setFieldType(targetVo.getFieldType());
					basic.setFieldSort(num + i + 1);
					labItemsTemp2BasicService.save(basic);

				}
			}
		} else {
			for (int i = 0; i < itemsTargetList.size(); i++) {
				LabItemsTargetVo targetVo = itemsTargetList.get(i);
				LabItemsTemp2Basic basic = new LabItemsTemp2Basic();
				basic.setItemsId(itemsId);
				basic.setFieldName(targetVo.getFieldName());
				basic.setFieldCode(targetVo.getFieldCode());
				basic.setFieldType(targetVo.getFieldType());
				basic.setFieldSort(i + 1);
				labItemsTemp2BasicService.save(basic);

			}
		}

		if (MyEnum.sync){
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_items_temp2_basic","0",time,1);
			labCronJobService.save(job);
		}
		return Result.OK("基础信息添加成功！");
	}


    /**
    * 导出excel
    *
    * @param request
    * @param labItemsTemp2Basic
    */
    @RequiresPermissions("basic:lab_items_temp2_basic:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabItemsTemp2Basic labItemsTemp2Basic) {
        return super.exportXls(request, labItemsTemp2Basic, LabItemsTemp2Basic.class, "实验项目模板-基础信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("basic:lab_items_temp2_basic:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabItemsTemp2Basic.class);
    }

}
